package cn.gson.crm.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.pojo.ConstactOrder;
import cn.gson.crm.model.pojo.OrderResult;
import cn.gson.crm.model.pojo.Orderdetails1;
import cn.gson.crm.model.vo.ConstactOrderVO;
import cn.gson.crm.model.vo.coodVO;
import cn.gson.crm.service.ConstractOrderService;
import cn.gson.crm.service.OrderDetailsService;

@Controller
public class ConstractOrderControl {
	@Autowired
	private ConstractOrderService cService;
	@Autowired
	private OrderDetailsService oService;
	
	/**
	 * 合同订单Json
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/getAllOrder")
	@ResponseBody
	public Map getAllOrder(int page, int rows) {
		long count = cService.orderCount();
		List<ConstactOrderVO> list = cService.getAllOrder(page, rows);
		Map map = new HashMap<>();
		map.put("total", count);
		map.put("rows", list);
		return map;
	}

	@GetMapping("/getOrderByTheme")
	@ResponseBody
	public List<ConstactOrderVO> getOrderByTheme(String coTheme) {
		return cService.getOrderByThemeLike(coTheme);
	}

	@RequestMapping("/getOrderSome")
	@ResponseBody
	public Map getOrderSome(ConstactOrderVO con,int page, int rows) {
		System.out.println("a"+con.getCode()+"b");
		Map map = new HashMap<>();
		Long count = cService.orderCount();
		PageHelper pHelper = new PageHelper();
		pHelper.startPage(page, rows);
		PageInfo<ConstactOrderVO> pInfo = new PageInfo<>(cService.getSomeOrder(con));
		map.put("total", count);
		map.put("rows", pInfo.getList());
		return map;
	}
	
	@PostMapping("/addCood")
	@ResponseBody
	public Map addCood(coodVO vo){
		Map map = new HashMap<>();
		try {
			System.out.println(vo);
			cService.addCood(vo.getcOrder(), vo.getOrderdetails1s());
			map.put("success", true);
		} catch (Exception e) {
			map.put("success", false);
			e.printStackTrace();
		}
		return map;
	}

	@GetMapping("/go_to_Order")
	public String go_to_Order() {
		return "zj/order";
	}

	@GetMapping("/addOrder")
	public String addOrder() {
		return "zj/addOrder";
	}
	
	@GetMapping("/toCood")
	public String toCood(){
		return "zj/cood";
	}

	@PostMapping("/addNewOrder")
	@ResponseBody
	public OrderResult addNewOrder(ConstactOrder con) {
		try {
			cService.addOrder(con);
			return new OrderResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new OrderResult(e);
		}
	}

	@PostMapping("/editorder")
	@ResponseBody
	public OrderResult editOrder(ConstactOrder con) {
		System.out.println(con.getCoId());
		try {
			cService.editOrder(con);
			return new OrderResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new OrderResult(e);
		}
	}

	@GetMapping("/getOrderById")
	@ResponseBody
	public ConstactOrderVO getOrderById(long coId) {
		return cService.getOrderById(coId);
	}

	@GetMapping("/getOrderDetailsById")
	@ResponseBody
	public List<Orderdetails1> getOrderDetailsById(long coId) {
		return oService.getDetailsByOid(coId);
	}

	@GetMapping("/deleteOrder")
	@ResponseBody
	public OrderResult deleteOrder(long coId) {
		try {
			cService.deleteOrder(coId);
			return new OrderResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new OrderResult(e);
		}
	}
}
